home *** CD-ROM | disk | FTP | other *** search
Text File | 1999-06-26 | 3.9 KB | 165 lines | [TEXT/CWIE] |
- // ===========================================================================
- // CTCPResponderThread.cp ©1995-1998 Metrowerks Inc. All rights reserved.
- // ===========================================================================
- // This is the threaded version of the responder process.
-
- #include "CTCPResponderThread.h"
- #include "CTCPResponder.h"
-
-
- //ej
- #include "ScreenGrabber.h"
-
- #include <cstring>
-
- #define kServerClosingMessage "Server closing down... Goodbye"
-
-
- // ---------------------------------------------------------------------------
- // • CTCPResponderThread
- // ---------------------------------------------------------------------------
- // Constructor
-
- CTCPResponderThread::CTCPResponderThread(
- PP_PowerPlant::LTCPEndpoint* inNetworkEndpoint,
- CTerminalPane* inTerminalPane,
- CTCPResponder* inResponderMaster,
- CSimpleTCPServer* inMasterServer)
- : LThread( false,
- PP_PowerPlant::thread_DefaultStack,
- PP_PowerPlant::LThread::threadOption_Default,
- nil),
- mEndpoint(inNetworkEndpoint),
- mTerminalPane(inTerminalPane),
- mResponderMaster(inResponderMaster),
- mMasterServer(inMasterServer)
- {
- mContinue = true;
- mInDisconnect = false;
- mEndpoint->AddListener(this);
- }
-
- // ---------------------------------------------------------------------------
- // • ~CTCPResponderThread
- // ---------------------------------------------------------------------------
- // Destructor
-
- CTCPResponderThread::~CTCPResponderThread()
- {
- }
-
-
- // ---------------------------------------------------------------------------
- // • ListenToMessage
- // ---------------------------------------------------------------------------
- void
- CTCPResponderThread::ListenToMessage(
- PP_PowerPlant::MessageT inMessage,
- void *ioParam)
- {
- #pragma unused (ioParam)
-
- switch (inMessage) {
- case T_DISCONNECT:
- mContinue = false;
- break;
-
- case T_ORDREL:
- mContinue = false;
- if (mInDisconnect) {
- this->Resume();
- }
- mInDisconnect = true; //if not in a disconnect sequence we are now
- break;
- }
- }
-
- // ---------------------------------------------------------------------------
- // • StartDisconnect
- // ---------------------------------------------------------------------------
-
- void
- CTCPResponderThread::StartDisconnect()
- {
- if (mContinue) {
- mContinue = false;
- mEndpoint->AbortThreadOperation(this);
- }
- }
-
- // ---------------------------------------------------------------------------
- // • Run
- // ---------------------------------------------------------------------------
-
- void*
- CTCPResponderThread::Run()
- {
-
- Handle h = nil;
-
- try {
- // The endpoint is automagically bound to the port on which incomming connection
- // was recieved via the 'AcceptIncoming' event.
-
- mMasterServer->GetEndPoint()->AcceptIncoming(mEndpoint);
- mMasterServer->AddToConnectionCount(1);
-
- mResponderMaster->BindCompleted();
-
- //ej
- SInt16 screen_depth = 16;
- OSErr result = noErr;
-
- //ej
- result = GetScreenJPEG(h, screen_depth);
- if (result == noErr )
- {
-
- HLockHi(h);
-
- SInt32 i;
- char *ptr = *h;
- SInt32 length = GetHandleSize(h);
-
- for(i=0; i<length; i+= 4096)
- {
- mEndpoint->SendData(ptr, 4096 );
- ptr += 4096;
- }
-
- mEndpoint->SendData(ptr, length - (i - 4096) );
-
- HUnlock(h);
- }
- }
- catch(...) {
- }
-
- if ( h != nil) {
- DisposeHandle(h);
- }
-
- // We're done with this connection… disconnect as appropriate.
- try {
- if (!mInDisconnect) { //We must be initiating disconnect
- //Tell remote machine we are closing
- mEndpoint->Send(kServerClosingMessage, strlen(kServerClosingMessage));
- mInDisconnect = true;
- mEndpoint->SendDisconnect();
- Suspend(); //wait for T_ORDREL message
- // NOTE: You could continue to read data from the remote connection
- // at this point (instead of Suspending the thread) if you care about
- // receiving all it's data.
- } else {
- mEndpoint->Disconnect();
- }
- mEndpoint->Unbind();
- }
- catch(...) {
- }
-
- mMasterServer->AddToConnectionCount(-1);
- mResponderMaster->ServerThreadDied();
- return nil;
- }
-